/* 819. 最常见的单词 */
/**
 * @param {string} paragraph
 * @param {string[]} banned
 * @return {string}
 */
var mostCommonWord = function (paragraph, banned) {
    paragraph += ","
    const temp = []
    const isLetter = (letter) => letter >= 'a' && letter <= 'z'
    for (let i = 0, len = paragraph.length; i < len; i++) {
        let j = i
        let cur = ""
        while (j < len) {
            const _cur = paragraph[j].toLowerCase()
            if (isLetter(_cur)) {
                j++
                cur += _cur
            } else {
                i = j
                cur && !banned.includes(cur) && temp.push(cur)
                break
            }
        }
    }
    const map = new Map()
    for (let i = 0; i < temp.length; i++) {
        map.set(temp[i], (map.get(temp[i]) || 0) + 1)
    }
    let result = "", max = 0
    for (const [v1, v2] of map) {
        if (v2 > max) {
            result = v1
            max = v2
        }
    }
    return result
};
var mostCommonWord = function (p, forbid) {
    let obj = {};
    /* 
        \W:匹配字母，数字，下划线 => [A-Za-z0-9_]
    */
    p.toLowerCase().split(/\W+/).forEach(n => {
        obj[n] = (obj[n] || 0) + 1;
    })
    forbid.forEach(n => obj[n] = 0);
    return Object.keys(obj).sort((a, b) => obj[b] - obj[a])[0];
}
const result = mostCommonWord("a, a, a,b,b,c", [])

console.log("mostCommonWord :>>", result);
